package cn.ekuma.epos.datalogic.businesslogic.orderprocess.impl;

import java.util.Date;

import com.openbravo.bean.erp.Order;
import com.openbravo.bean.erp.OrderRelation;
import com.openbravo.bean.erp.OrderState;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.SerializerWriteString;
import com.openbravo.data.loader.StaticSentence;

import cn.ekuma.epos.datalogic.I_DataLogicERP;
import cn.ekuma.epos.datalogic.businesslogic.orderprocess.AbstractOrderProcess;
import cn.ekuma.epos.datalogic.define.dao.erp.OrderDAO;
import cn.ekuma.epos.datalogic.define.dao.erp.OrderRelationDAO;
import cn.ekuma.epos.datalogic.define.dao.erp.OrderStateDAO;

public class OrderReferenceOrderProcess extends AbstractOrderProcess {

	@Override
	public void saveOrder(I_DataLogicERP dlERP, Order order)
			throws BasicException {
		if (order.getRefOrders() != null) {
			OrderRelationDAO orderRelationDAO=(OrderRelationDAO)dlERP.getDao(OrderRelation.class);
			OrderStateDAO orderStateDAO=(OrderStateDAO) dlERP.getDao(OrderState.class);
			OrderDAO orderDAO=(OrderDAO) dlERP.getDao(Order.class);
            for (final OrderRelation orderRelation : order.getRefOrders()) {
                if(orderRelation.getAfterState()!=null&&!orderRelation.getAfterState().isEmpty()){
                	OrderState refOrderState=new OrderState();
                	refOrderState.setFireDate(new Date());
                	refOrderState.setM_User(order.getUser());
                	refOrderState.setOrderID(orderRelation.getRelOrderInfo().getId());
                	refOrderState.setRefId(order.getId());
                	refOrderState.setMemo(orderRelation.getMemo());
                	refOrderState.setState(orderRelation.getAfterState());
                	orderStateDAO.insert(refOrderState);
                	orderDAO.updateOrderState(orderRelation.getRelOrderInfo().getId(), orderRelation.getAfterState());
                }
                if(orderRelation.getCash()!=0.0)
                	orderDAO.updateOrderDebt(orderRelation.getRelOrderInfo().getId(), orderRelation.getCash());
            }
        }

	}

	@Override
	public void deleteOrder( I_DataLogicERP dlERP, Order order)
			throws BasicException {
		if (order.getRefOrders() != null) {
			OrderRelationDAO orderRelationDAO=(OrderRelationDAO)dlERP.getDao(OrderRelation.class);
			OrderStateDAO orderStateDAO=(OrderStateDAO) dlERP.getDao(OrderState.class);
			OrderDAO orderDAO=(OrderDAO) dlERP.getDao(Order.class);
            for (final OrderRelation orderRelation : order.getRefOrders()) {
                if(orderRelation.getAfterState()!=null&&!orderRelation.getAfterState().isEmpty()){
                	OrderState refOrderState=new OrderState();
                	refOrderState.setFireDate(new Date());
                	refOrderState.setM_User(order.getUser());
                	refOrderState.setOrderID(orderRelation.getRelOrderInfo().getId());
                	refOrderState.setRefId(order.getId());
                	refOrderState.setMemo(orderRelation.getMemo());
                	refOrderState.setState(orderRelation.getBeforState());
                	orderStateDAO.insert(refOrderState);
                	orderDAO.updateOrderState(orderRelation.getRelOrderInfo().getId(), orderRelation.getBeforState());
                }
                if(orderRelation.getCash()!=0.0)
                	orderDAO.updateOrderDebt(orderRelation.getRelOrderInfo().getId(), -orderRelation.getCash());
            }
            new StaticSentence(dlERP.getSession(), "DELETE FROM ORDERRELATIONS WHERE ORDERID = ?", SerializerWriteString.INSTANCE).exec(order.getId());
        }
		
		
	}






	@Override
	public String getName() {
		return "OrderReferenceOrderProcess";
	}
}
